package cn.nwpu.eckel.model;

import java.sql.Date;

import cn.nwpu.eckel.Dao.MPSDao;
import cn.nwpu.eckel.Dao.OrderDao;

public class Order extends Source{
	public Order(){
		this.id=0;
		this.mpsNo=0;
		this.ts=0;
		this.quantity=0;
		this.people="";
		this.buildTime=new Date(new java.util.Date().getTime());
	}
	public Order(int mpsNo,int ts,int quantity,String people){
		this.mpsNo=mpsNo;
		this.ts=ts;
		this.quantity=quantity;
		this.people=people;
		this.buildTime=new Date(new java.util.Date().getTime());
	}
	public Order(int id,int mpsNo,int ts,int quantity,String people){
		this.id=id;
		this.mpsNo=mpsNo;
		this.ts=ts;
		this.quantity=quantity;
		this.people=people;
	}
	public Order(int id,int mpsNo,int ts,int quantity,String people,Date buildTime){
		this.id=id;
		this.mpsNo=mpsNo;
		this.ts=ts;
		this.quantity=quantity;
		this.people=people;
		this.buildTime=buildTime;
	}
	
	
	public void add(){
		OrderDao.insertOrder(this);
	}
	
	public Source updateSelf(){
		Source s;
		s=OrderDao.alterOrder(this);
		return s;
	}
	
	public void afterAdd(){
		MPS mps=MPSDao.getMPSById(this.mpsNo);
		mps.loadPlans();
		mps.p[this.ts].odr+=this.quantity;
		mps.calPlans();
		mps.savePlans();
	}
	public void afterDelete(){
		MPS mps=MPSDao.getMPSById(this.mpsNo);
		mps.loadPlans();
		mps.p[this.ts].odr-=this.quantity;
		mps.calPlans();
		mps.savePlans();
	}
	public void afterAlter(Source oldOdr){
		MPS mps=MPSDao.getMPSById(this.mpsNo);
		mps.loadPlans();
		mps.p[this.ts].odr+=this.quantity-oldOdr.quantity;
		mps.calPlans();
		mps.savePlans();
	}
}
